package com.tan.mypark.adapter;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.tan.mypark.R;
import com.tan.mypark.module.AndroidDto;

import java.util.List;

public class PlateLincenerAdapter extends RecyclerView.Adapter<PlateLincenerAdapter.ViewHoder> {

    private Context context;
    private List<AndroidDto> dataList;
    private String directionType; // 区分左右列表，用于设置箭头
    private boolean isMarqueeStarted = false;

    public PlateLincenerAdapter(Context context, List<AndroidDto> dataList, String directionType) {
        this.context = context;
        this.dataList = dataList;
        this.directionType = directionType;
    }

    @NonNull
    @Override
    public ViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_plate_item, parent, false);
        return new ViewHoder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHoder holder, int position) {
//        AndroidDto dto = dataList.get(position);
//        holder.tvPlateLeft.setText(dto.getPlateNumber());
//
//        // 根据方向类型设置箭头图片
//        if ("left".equals(directionType)) {
//            holder.ivPlateLeft.setImageResource(R.mipmap.icon_left);
//        } else {
//            if ("右".equals(dto.getDirection())) {
//                holder.ivPlateLeft.setImageResource(R.mipmap.icon_right);
//            } else if ("上".equals(dto.getDirection())) {
//                holder.ivPlateLeft.setImageResource(R.mipmap.icon_up);
//            }
//        }
        AndroidDto dto = dataList.get(position);
        holder.tvPlateLeft.setText(dto.getPlateNumber());
        if (dto.getRgbValue() != null && !dto.getRgbValue().isEmpty()) {
            try {
                // 分割RGB值并转换为整数
                String[] rgbArray = dto.getRgbValue().split(",");
                if (rgbArray.length == 3) {
                    int r = Integer.parseInt(rgbArray[0].trim());
                    int g = Integer.parseInt(rgbArray[1].trim());
                    int b = Integer.parseInt(rgbArray[2].trim());

                    // 使用Color.rgb方法创建颜色
                    holder.tvPlateLeft.setTextColor(Color.rgb(r, g, b));
                    holder.ivPlateLeft.setColorFilter(Color.rgb(r,g,b));
                } else {
                    Log.e(TAG, "Invalid RGB format: " + dto.getRgbValue());
                    holder.tvPlateLeft.setTextColor(Color.WHITE); // 默认白色
                }
            } catch (NumberFormatException e) {
                Log.e(TAG, "Failed to parse RGB values: " + dto.getRgbValue(), e);
                holder.tvPlateLeft.setTextColor(Color.WHITE); // 默认白色
            }
        } else {
            holder.tvPlateLeft.setTextColor(Color.WHITE); // 默认白色
        }

        // 获取父布局
        ViewGroup parent = (ViewGroup) holder.tvPlateLeft.getParent();

        // 根据方向类型 + 数据方向设置箭头
        if (directionType != null && "left".equals(directionType)) {
            if (dto.getDirection() != null&&"左".equals(dto.getDirection())){
                holder.ivPlateLeft.setImageResource(R.mipmap.icon_left);
            }
            // 保存视图的LayoutParams
            LinearLayout.LayoutParams tvParams = (LinearLayout.LayoutParams) holder.tvPlateLeft.getLayoutParams();
            LinearLayout.LayoutParams ivParams = (LinearLayout.LayoutParams) holder.ivPlateLeft.getLayoutParams();
            // 移除视图
            parent.removeView(holder.tvPlateLeft);
            parent.removeView(holder.ivPlateLeft);
            // 按新顺序添加视图
            parent.addView(holder.ivPlateLeft, 0, ivParams);
            parent.addView(holder.tvPlateLeft, 1, tvParams);
        } else if (dto != null && dto.getDirection() != null){
            switch (dto.getDirection()) {
                case "右":
                    holder.ivPlateLeft.setImageResource(R.mipmap.icon_right);
                    break;
                case "直":
                    holder.ivPlateLeft.setImageResource(R.mipmap.icon_up);
                    break;
                default:
                    holder.ivPlateLeft.setImageResource(R.mipmap.icon_right);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class ViewHoder extends RecyclerView.ViewHolder {
        TextView tvPlateLeft;
        ImageView ivPlateLeft;

        public ViewHoder(@NonNull View itemView) {
            super(itemView);
            tvPlateLeft = itemView.findViewById(R.id.tvPlateLeft);
            ivPlateLeft = itemView.findViewById(R.id.ivPlateLeft);
        }
    }
}